<?php

/**
 * 后台操作日志模型
 * @author yupoxiong<i@yufuping.com>
 */

namespace app\admin\model;

use think\model\relation\BelongsTo;
use think\model\relation\HasOne;
use tools\Crypt;

class AdminLog extends Model
{
    protected $name = 'admin_log';

    public $softDelete = false;

    public $methodText = [
        1 => 'GET',
        2 => 'POST',
        3 => 'PUT',
        4 => 'DELETE',
    ];

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;

    protected $searchField = [
        'name',
        'url',
        'log_ip'
    ];

    protected $whereField = [
        'admin_user_id'
    ];

    protected $timeField = [
        'create_time'
    ];

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->name = 'admin_log_' . date('Ym');
    }

    public function setMonth($month)
    {
        $this->name = 'admin_log_' . $month;
        return $this;
    }


    //关联用户
    public function adminUser(): BelongsTo
    {
        return $this->belongsTo(AdminUser::class);
    }
    public function getContentAttr($value)
    {
        $data = Crypt::decrypt($value, setting('admin.safe.admin_key'));
        return json_encode(json_decode($data, true), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
    }
    

}
